﻿using System;
using System.Collections;
using ReadyApps.Wiki.ViewModels;

namespace ReadyApps.Wiki
{
    [Serializable]
    class WikiPageState : IWikiPageState
    {
        #region Variables and Constants

        private const int BITS_INITIALIZED = 0;
        private const int BITS_PAGE_FOUND = 1;
        private const int BITS_SITE_FOUND = 2;

        private BitArray bits;

        #endregion

        #region Properties

        public bool IsInitialized
        {
            get { return bits[BITS_INITIALIZED]; }
            private set { bits[BITS_INITIALIZED] = value; }
        }

        public bool SiteFound
        {
            get { return bits[BITS_SITE_FOUND]; }
            private set { bits[BITS_SITE_FOUND] = value; }
        }

        public bool PageFound
        {
            get { return bits[BITS_PAGE_FOUND]; }
            private set { bits[BITS_PAGE_FOUND] = value; }
        }

        public int PageID { get; private set; }

        public int RevisionNumber { get; set; }

        public WikiPageMode PageMode { get; private set; }

        public IWikiViewModel ViewModel { get; set; }

        #endregion

        #region Constructor

        public WikiPageState()
        {
            bits = new BitArray(3);
            IsInitialized = false;
            PageMode = WikiPageMode.Unknown;
            RevisionNumber = WikiController.HEAD_REVISION;
        }

        #endregion

        #region Public Methods

        public void Initialize(int pageID)
        {
            PageID = pageID;
            SiteFound = true;
            PageFound = true;
            IsInitialized = true;
        }

        public void InitializeSiteNotFound()
        {
            SiteFound = false;
            PageFound = false;
            IsInitialized = true;
        }

        public void InitializePageNotFound()
        {
            SiteFound = true;
            PageFound = false;
            IsInitialized = true;
        }

        public void ChangePageMode(WikiPageMode newMode)
        {
            ViewModel = null;
            PageMode = newMode;
        }

        public void Reset()
        {
            IsInitialized = false;
        }

        #endregion
    }
}
